package me.zhengjie.modules.customer_app.rest;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.customer.domain.TCustomerUser;
import me.zhengjie.modules.customer.service.TCustomerInfoService;
import me.zhengjie.modules.customer.service.dto.TCustomerInfoDTO;
import me.zhengjie.modules.customer.service.mapper.TCustomerUserMapper;
import me.zhengjie.modules.monitor.service.RedisService;
import me.zhengjie.modules.security.security.AuthenticationInfo;
import me.zhengjie.modules.security.security.AuthenticationInfoEx;
import me.zhengjie.modules.security.security.AuthorizationUser;
import me.zhengjie.modules.security.security.JwtUser;
import me.zhengjie.modules.security.utils.JwtTokenUtil;
import me.zhengjie.utils.EncryptUtils;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by bruce on 19/9/27.
 */
@Slf4j
@RestController
@RequestMapping("/app/customer")
public class TAppCustomerAuthController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    @Qualifier("jwtCustomerUserDetailsService")
    private UserDetailsService userDetailsService;

    @Autowired
    private TCustomerInfoService tCustomerInfoService;

    /**
     * 验证密码
     * @param authorizationUser
     * @return
     */
    @Log("客户登录")
    @PostMapping(value = "/login")
    public ResponseEntity login(@Validated @RequestBody AuthorizationUser authorizationUser){
        /*
        // 查询验证码
        String code = redisService.getCodeVal(authorizationUser.getUuid());
        // 清除验证码
        redisService.delete(authorizationUser.getUuid());
        if (StringUtils.isBlank(code)) {
            throw new BadRequestException("验证码已过期");
        }
        if (StringUtils.isBlank(authorizationUser.getCode()) || !authorizationUser.getCode().equalsIgnoreCase(code)) {
            throw new BadRequestException("验证码错误");
        }*/
        final JwtUser jwtUser = (JwtUser) userDetailsService.loadUserByUsername(authorizationUser.getUsername());

        //if(!jwtUser.getPassword().equals(EncryptUtils.encryptPassword(authorizationUser.getPassword()))){
        if(!jwtUser.getPassword().equals(authorizationUser.getPassword())){
            throw new AccountExpiredException("密码错误");
        }

        if(!jwtUser.isEnabled()){
            throw new AccountExpiredException("账号已停用，请联系管理员");
        }

        // 生成令牌
        final String token = jwtTokenUtil.generateToken(jwtUser);

        TCustomerInfoDTO customerInfo = tCustomerInfoService.findById(jwtUser.getOwnerId()); //归属企业
        JSONObject object = new JSONObject();
        object.put("companyName",customerInfo.getCustomerName());
        object.put("address", customerInfo.getAddress());

        // 返回 token
        return ResponseEntity.ok(new AuthenticationInfoEx(token,jwtUser,object));
    }
}
